package net.sunzc.takephoto;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.Toast;
import com.hanvon.faceRec.OUserInfo;
import net.sunzc.hwfacetest.R;
import net.sunzc.takephoto.utils.MyLog;

public class FaceRecognizeActivity extends Activity implements FaceRecognizer.RecognizeListener {

    private static final String FACE_DATA = "face_data";
    private static final int RETRY = 5;
    private static final String TAG = "FaceRecognizeActivity";
    private FaceRecognizer faceRecognize;

    public static void open(Context context, String faceData) {
        Intent intent = new Intent(context, FaceRecognizeActivity.class);
        intent.putExtra(FACE_DATA, faceData);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_face_recognize);
        String faceData = getIntent().getStringExtra(FACE_DATA);
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view);
        ICamera camera = MyLog.getCamera(this, surfaceView);
        faceRecognize = FaceRecognizer.getRecognize(this, camera, faceData);
        faceRecognize.setFaceListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        faceRecognize.exit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        faceRecognize.recognizeFace();
    }

    @Override
    public void onExit() {

    }

    private int count;

    @Override
    public void onRecognizeFailure(FaceRecognizer faceRecognizer) {
        if (count > RETRY) {
            return;
        }
        count++;
        faceRecognizer.recognizeFace();
    }

    @Override
    public void onRecognizeSuccess(FaceRecognizer faceRecognizer, Bitmap bitmap, OUserInfo userInfo) {
        Toast.makeText(this, "识别成功:" + userInfo, Toast.LENGTH_SHORT).show();
        MyLog.i(TAG, "识别成功:" + userInfo);
    }
}
